<?php


class AdminBase extends CI_Controller{
	
	public function __construct(){
          parent::__construct();
         $this->load->library('session');
         $data=$this->session->userdata('userinfo');
          
         $wxapp=$this->db->get('wxapp')->row_array();
         $this->session->set_userdata('wxapp',$wxapp);  
          
          if(is_null($data)){
          	header("Location:/login/index");
          }else{
          	$info=$this->db->where('id',$data['id'])->get('admin')->row_array();
           if(session_id()!==$info['session_id']){
    		header("Location:/login/error");
           }
          }
    	}
	
	

	public function GetId(){
	  return (int)$this->uri->segment(3);
	}

	public function PostId(){
		return (int)$this->input->post()['id'];
	}

	public function GetMenu($id=0){
		$list=$this->db->where('parent_id',$id)->get('menu')->result_array();
		return $list;
	}

	public function GetRole(){
		$list=$this->db->get('role')->result_array();
		return $list;
	}

	public function post(){
		if ($_SERVER['REQUEST_METHOD']=='POST'){
			return true;
		}else{
			return false;
		}
	}

	public function get(){
		if ($_SERVER['REQUEST_METHOD']=='GET'){
			return true;
		}else{
			return false;
		}
	}

	public function show($data){
		echo json_encode($data);
		exit;
	}


	public function uploadfile(){
		$pathurl='uploads/'.date('Y-m-d');
		if (!file_exists($pathurl)) {
			mkdir($pathurl,0777,true);
		}
		$config['upload_path']      = $pathurl;
		$config['allowed_types']    = 'gif|jpg|png|jpeg';
		$config['file_name']    =date('Ymd',time()).uniqid();
		$config['max_size']     = 2048;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('file')) {
			$data = array('code'=>0,'error'=>$this->upload->display_errors());
		} else {
			$data = array('code'=>1,'url'=>$pathurl.'/'.$config['file_name'],'upload_data'=>$this->upload->data());
		}
		return $data;
	}


	public function GetImg($id){
		$list=$this->db->where('id',$id)->get('file')->row_array();
		if ($list){
			return '/'.$list['url'];
		}
	}
	
	
		public function GetGoodsImage($goods_id){
		$where['goods_id']=$goods_id;
		$list=$this->db->where($where)->get('goods_image')->result_array();
		return $list;
	}

	

}



class ApiBase extends CI_Controller{

    
	public function show($data){
		echo json_encode($data);
		exit;
	}

	public function post(){
		if ($_SERVER['REQUEST_METHOD']=='POST'){
			return true;
		}else{
			return false;
		}
	}

	public function get(){
		if ($_SERVER['REQUEST_METHOD']=='GET'){
			return true;
		}else{
			return false;
		}
	}

	public function GetUser(){
		if ($this->post()){
			$token=$this->input->post()['token'];
		}else if ($this->get()){
			$token=$this->uri->segment(3);
		}
		if(empty($token)){
			$this->show(array('code'=>-1,'msg'=>'缺少token值'));
		}
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		$user=$this->cache->get($token);
		 if (!$user){
		 	$this->show(array('code'=>-1,'msg'=>'找不到用户信息'));
        }
        return $user;
	}

	public function GetImg($id){
		$list=$this->db->where('id',$id)->get('file')->row_array();
		if ($list){
			return 'https://'.$_SERVER['HTTP_HOST'].'/'.$list['url'];
		}
	}

	public function GetGoodsImage($goods_id){
		$where['goods_id']=$goods_id;
		$list=$this->db->where($where)->get('goods_image')->result_array();
		return $list;
	}
	
	public function getPrice($goods_id){
		$where['goods_id']=$goods_id;
		$list=$this->db->where($where)->get('goods_sku')->row_array();
		return $list['price'];
		
	}
	
	public function getSkuPrice($sku){
		$where['id']=$sku;
		$list=$this->db->where($where)->get('goods_sku')->row_array();
		return $list['price'];	
	}


}
